package tutorial.lite_mod.human_res;

import com.google.common.base.Joiner;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import public_util.model.*;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.util.sql.SelectTool;
import system_api_4_trade.util.tool_4_handle.StringToolApi;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 这个类用来提供给before和after方法使用
 * http://note.youdao.com/noteshare?id=d0f13adf003f4b3b896aefb9e1275e7b
 * 传递特殊参数, 可以演示的模板部分
 * 后面大部分的配置, 都是前端自己传过来比较稳妥
 * 每个传参对应一个相应的方法
 */
public class TemplateUtilApi {

    private static Logger logger = Logger.getLogger(TemplateUtilApi.class);


    /**
     * 验证链接: duzikai_api_mgr_public_agent_info?invoke_method=public_page_4m_load_agent_list&id_public_agent_info=99101&is_online=0&pre_cmd_para_show_human_res_agent_info=true
     * 工具零件方法
     * 组装方法: 主要是在before活着after方法里面调用
     * 根据这个零件来拼装
     * @return
     */
    public static ReturnData pre_cmd_para_show_human_res_agent_info(List<PublicAgentInfo> agentInfolist, HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db, ReturnData resultData)
    {
        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        String pre_cmd_para_show_human_res_agent_info = request.getParameter("pre_cmd_para_show_human_res_agent_info");
        if ("true".equals(pre_cmd_para_show_human_res_agent_info))
        {

            if (agentInfolist == null || agentInfolist.size() == 0)
            {
                return null;
            }

            for (int i = 0; i < agentInfolist.size(); i++)
            {
                PublicAgentInfo agentInfo = agentInfolist.get(i);
                agentInfo.setSave_data_005("");
                List<PublicMiddleTable> qiyeList = find_qiye_info(middleTableMapper, agentInfo.getId_public_agent_info() + "");
                if (qiyeList != null && qiyeList.size() > 0)
                {
                    agentInfo.setSave_data_005(qiyeList);
                }
            }


        }
        return null;
    }


    //查企业信息
    public static List<PublicMiddleTable> find_qiye_info(ApiPublicMiddleTableMapper midMapper, String id_public_agent_info)
    {
        HashMap<String, Object> paraPrice = new HashMap<String, Object>();
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 55001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_id_public_agent_info+ "_in", id_public_agent_info);
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraPrice, "public_middle_table_lite_mod_human_res");
        List<PublicMiddleTable> qiyeInfoList = midMapper.tableDataList(realPara);//查询是否具备值
        if (qiyeInfoList == null || qiyeInfoList.size() == 0)
        {
            return null;
        }
        return qiyeInfoList;
    }



    //查用户信息
    public static List<PublicMiddleTable> find_user_info(ApiPublicMiddleTableMapper midMapper, String id_public_user_info)
    {
        HashMap<String, Object> paraPrice = new HashMap<String, Object>();
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 52001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_id_public_user_info+ "_in", id_public_user_info);
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraPrice, "public_middle_table_lite_mod_human_res");
        List<PublicMiddleTable> userInfoList = midMapper.tableDataList(realPara);//查询是否具备值
        if (userInfoList == null || userInfoList.size() == 0)
        {
            return null;
        }
        return userInfoList;
    }



    /**
     * 验证链接: duzikai_api_mgr_public_agent_info?invoke_method=public_page_4m_load_agent_list&id_public_agent_info=99101&is_online=0&pre_cmd_para_filter_4_mid_table_remark_025=外资（非欧美）
     * 工具零件方法
     * 组装方法: 主要是在before活着after方法里面调用
     * 根据这个零件来拼装
     * @return
     */
    public static ReturnData pre_cmd_para_filter_4_mid_table_remark_025(HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db)
    {
        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        String pre_cmd_para_filter_4_mid_table_remark_025 = request.getParameter("pre_cmd_para_filter_4_mid_table_remark_025");
        pre_cmd_para_filter_4_mid_table_remark_025 = StringToolApi.paraHttpFilter(pre_cmd_para_filter_4_mid_table_remark_025);
        if (pre_cmd_para_filter_4_mid_table_remark_025 != null)
        {

            String id_list_agent = find_id_agent_4_r025(pre_cmd_para_filter_4_mid_table_remark_025, middleTableMapper);
            if (id_list_agent == null)
            {
                id_list_agent = "-1";//啥都没搜,也不能放过
            }
            params2db.put("id_public_agent_info_in", id_list_agent);

        }
        return null;
    }


    /**
     * 查询相应的agentId信息
     * @param middleTableMapper
     * @return
     */
    private static String find_id_agent_4_r025(String r025, ApiPublicMiddleTableMapper middleTableMapper)
    {
        HashMap<String, Object> paraPrice = new HashMap<String, Object>();
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 55001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_mid_table_remark_025+ "_in", r025);
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraPrice, "public_middle_table_lite_mod_human_res");
        List<PublicMiddleTable> listQianyuePrice = middleTableMapper.tableDataList(realPara);//查询是否具备值
        if (listQianyuePrice == null || listQianyuePrice.size() == 0)
        {
            return null;
        }
        //返回逗号隔开的ID
        List<Integer> listInteger = listQianyuePrice.stream().map(PublicMiddleTable::getId_public_agent_info).collect(Collectors.toList());
        String list_id_str = Joiner.on(",").join(listInteger);
        return list_id_str;
    }


    /**
     * 查询相应的用户表信息
     * @param middleTableMapper
     * @return
     */
    private static String find_id_user_4_r002(String r002, ApiPublicMiddleTableMapper middleTableMapper)
    {
        HashMap<String, Object> paraPrice = new HashMap<String, Object>();
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 52001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_mid_table_remark_002+ "_in", r002);
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraPrice, "public_middle_table_lite_mod_human_res");
        List<PublicMiddleTable> listQianyuePrice = middleTableMapper.tableDataList(realPara);//查询是否具备值
        if (listQianyuePrice == null || listQianyuePrice.size() == 0)
        {
            return null;
        }
        //返回逗号隔开的ID
        List<Integer> listInteger = listQianyuePrice.stream().map(PublicMiddleTable::getId_public_user_info).collect(Collectors.toList());
        String list_id_str = Joiner.on(",").join(listInteger);
        return list_id_str;
    }




    /**
     * 验证链接: duzikai_api_mgr_public_user_info?invoke_method=public_page_4m_load_user_list&id_public_user_info=169&pre_cmd_para_show_human_res_user_info=true
     * 工具零件方法
     * 组装方法: 主要是在before活着after方法里面调用
     * 根据这个零件来拼装
     * @return
     */
    public static ReturnData pre_cmd_para_show_human_res_user_info(List<PublicUserInfo> userInfoList, HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db, ReturnData resultData)
    {
        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        String pre_cmd_para_show_human_res_user_info = request.getParameter("pre_cmd_para_show_human_res_user_info");
        if ("true".equals(pre_cmd_para_show_human_res_user_info))
        {

            if (userInfoList == null || userInfoList.size() == 0)
            {
                return null;
            }
            for ( int i = 0; i < userInfoList.size(); i++)
            {
                PublicUserInfo userInfo = userInfoList.get(i);
                userInfo.setSave_data_005("");
                List<PublicMiddleTable> user_infoList = find_user_info(middleTableMapper, userInfo.getId_public_user_info() + "");
                if (user_infoList != null && user_infoList.size() > 0)
                {
                    userInfo.setSave_data_005(user_infoList);
                }
            }

        }
        return null;
    }



    /**
     * 验证链接: duzikai_api_mgr_public_user_info?invoke_method=public_page_4m_load_user_list&id_public_user_info=169&pre_cmd_para_filter_4_mid_table_remark_002=全职
     * 工具零件方法
     * 组装方法: 主要是在before活着after方法里面调用
     * 根据这个零件来拼装
     * @return
     */
    public static ReturnData pre_cmd_para_filter_4_mid_table_remark_002(HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db)
    {
        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        String pre_cmd_para_filter_4_mid_table_remark_002 = request.getParameter("pre_cmd_para_filter_4_mid_table_remark_002");
        pre_cmd_para_filter_4_mid_table_remark_002 = StringToolApi.paraHttpFilter(pre_cmd_para_filter_4_mid_table_remark_002);
        if (pre_cmd_para_filter_4_mid_table_remark_002 != null)
        {

            String id_list_agent = find_id_user_4_r002(pre_cmd_para_filter_4_mid_table_remark_002, middleTableMapper);
            if (id_list_agent == null)
            {
                id_list_agent = "-1";//啥都没搜,也不能放过
            }
            params2db.put("id_public_user_info_in", id_list_agent);

        }
        return null;
    }


}

